package cn.itsource.pethome.controller;

import cn.itsource.pethome.domain.FileName;
import cn.itsource.pethome.result.JSONResult;
import cn.itsource.pethome.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理API
 */
@RestController
@RequestMapping("/file")
public class FileController{

    @Autowired
    private IFileService fileService ;

    //文件上传
    @PostMapping("/fastdfs/upload")
    public JSONResult upload(MultipartFile file){
        return fileService.upload2Fastdfs(file);
    }

    @PostMapping("upload")
    public JSONResult fileUpload(@RequestParam("fileName") MultipartFile file){
        return JSONResult.success(fileService.fileUpload(file));
    }
    //文件删除
    @PostMapping("/deleteFile")
    public JSONResult deleteFile(@RequestBody FileName fileName) {
        String url = fileName.getUrl();
        int i = url.lastIndexOf("/");
        String substring = url.substring(i+1);
        fileService.fileDelete(substring);
        return JSONResult.success();
    }
}